/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.mojo.basics.log;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.maven.plugin.logging.Log;

/**
 * Allow to use log4j to log to a maven plugin logger.
 */
public class Log4jMavenAppender
    extends AppenderSkeleton
{

  /**
   * the maven plugin logger to log to.
   */
  private Log mavenLog;

  /**
   * the class of the maven plugin that will call this appender.
   */
  private String mavenPluginClassName;



  /**
   * getter for the mavenLog property.
   * 
   * @return the mavenLog
   */
  public Log getMavenLog()
  {
    if (mavenLog == null) {
      try {
        final Class<?> mavenPluginClass = Class.forName(mavenPluginClassName);
        mavenLog = (Log) MethodUtils.invokeExactStaticMethod(mavenPluginClass, "getMavenLogger", new Object[0]);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    return mavenLog;
  }



  /**
   * setter for the mavenLog property.
   * 
   * @param mavenLog
   *          the mavenLog to set
   */
  public void setMavenLog(Log mavenLog)
  {
    this.mavenLog = mavenLog;
  }



  /**
   * method that perform actual logging.
   * 
   * @param event
   *          the event to log
   * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
   */
  @Override
  protected void append(LoggingEvent event)
  {
    if (getMavenLog() == null) {
      System.err.println("Unable to retrieve mavenLog");
      return;
    }
    switch (event.getLevel().toInt()) {
      case Level.TRACE_INT:
        break;
      case Level.DEBUG_INT:
        if (event.getThrowableInformation() == null) {
          getMavenLog().debug(event.getRenderedMessage());
        }
        else {
          getMavenLog().debug(event.getRenderedMessage(), event.getThrowableInformation().getThrowable());
        }
        break;

      case Level.INFO_INT:
        if (event.getThrowableInformation() == null) {
          getMavenLog().info(event.getRenderedMessage());
        }
        else {
          getMavenLog().info(event.getRenderedMessage(), event.getThrowableInformation().getThrowable());
        }
        break;

      case Level.WARN_INT:
        if (event.getThrowableInformation() == null) {
          getMavenLog().warn(event.getRenderedMessage());
        }
        else {
          getMavenLog().warn(event.getRenderedMessage(), event.getThrowableInformation().getThrowable());
        }
        break;

      case Level.ERROR_INT:
      case Level.FATAL_INT:
        if (event.getThrowableInformation() == null) {
          getMavenLog().error(event.getRenderedMessage());
        }
        else {
          getMavenLog().error(event.getRenderedMessage(), event.getThrowableInformation().getThrowable());
        }
        break;
    }
  }



  /**
   * Release any resources allocated within the appender such as file handles,
   * network connections, etc.
   * 
   * @see org.apache.log4j.AppenderSkeleton#close()
   */
  @Override
  public void close()
  {
    // Do nothing.
  }



  /**
   * Configurators call this method to determine if the appender requires a
   * layout. If this method returns true, meaning that layout is required, then
   * the configurator will configure an layout using the configuration
   * information at its disposal. If this method returns false, meaning that a
   * layout is not required, then layout configuration will be skipped even if
   * there is available layout configuration information at the disposal of the
   * configurator.
   * 
   * @return wether the appender needs layout.
   * @see org.apache.log4j.AppenderSkeleton#requiresLayout()
   */
  @Override
  public boolean requiresLayout()
  {
    return true;
  }



  /**
   * setter for the mavenPluginClassName property.
   * 
   * @param mavenPluginClassName
   *          the mavenPluginClassName to set
   */
  public void setMavenPluginClassName(String mavenPluginClassName)
  {
    this.mavenPluginClassName = mavenPluginClassName;
  }

}
